/**
 * Created by webhugo on 16-10-14.
 */
var path = require('path');
var koa = require('koa');
var app = new koa();
var koaValidate = require('koa-validate');
var staticServer = require('koa-static');
const session = require('koa-session')

var router = require('./router/index');
var koaBody = require('koa-body');
const responser = require('./lib/responser');
const config = require('./config/config');
var logger = require('./log/index').logger;

app.keys = ["education"]

const CONFIG = {
    key: 'oureda:bag', /** (string) cookie key (default is koa:sess) */
    /** (number || 'session') maxAge in ms (default is 1 days) */
    /** 'session' will result in a cookie that expires when session/browser is closed */
    /** Warning: If a session cookie is stolen, this cookie will never expire */
    maxAge: 86400000,
    overwrite: true, /** (boolean) can overwrite or not (default true) */
    httpOnly: true, /** (boolean) httpOnly or not (default true) */
    signed: false, /** (boolean) signed or not (default true) */
    rolling: false, /** (boolean) Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge, resetting the expiration countdown. default is false **/
}

app.use(session(CONFIG, app))

app.use(async(ctx, next)=> {
    try {
        await next();
    } catch (e) {
        if (process.env.NODE_ENV != 'development') {
            logger.error(e);
        }//在生产模式下把错误输入到文件中
        responser.catchErr(ctx, e);
    }
});
app.use(staticServer(path.join(__dirname, 'public')));
app.use(koaBody());
app.use(koaValidate());
app.use(router.routes());
app.listen(config.server.port);
